package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.UserWallet;
import com.hyk.dto.R;
import com.hyk.service.UserWalletService;
import com.hyk.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "提币地址控制器")
@RequestMapping("/userWallets")
public class UserWalletController {

    @Resource
    private UserWalletService userWalletService;

    @GetMapping
    @ApiOperation("提币地址列表")
    @PreAuthorize("hasAuthority('user_wallet_query')")
    public R<Page<UserWallet>> list(@ApiIgnore Page<UserWallet> page,
                                    String userId){
        QueryWrapper<UserWallet> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userId)){
            wrapper.eq("user_id",userId);
        }
        wrapper.orderByDesc("created");
        return R.ok(userWalletService.page(page,wrapper));
    }

    @GetMapping("/getCoinAddress/{coinId}")
    public R<List<UserWallet>> userList(@PathVariable("coinId") Long coinId){
        List<UserWallet> list = userWalletService
                .list(new LambdaQueryWrapper<UserWallet>()
                        .eq(UserWallet::getCoinId, coinId)
                        .eq(UserWallet::getUserId, UserUtils.getUser())
                        .orderByDesc(UserWallet::getCreated));
        return R.ok(list);
    }

    @PostMapping
    public R save(@RequestBody UserWallet userWallet){
        userWalletService.saveUserWallet(userWallet);
        return R.ok();

    }

}
